package com.org.manhu.chargehelper.Zxing;

import java.io.IOException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.ExifInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.hardware.Camera;

public class AlertApp {

	// 1：WIFI网络
	public static final int WIFI = 1;
	// 2：wap网络
	public static final int CMWAP = 2;
	// 3：net网络
	public static final int CMNET = 3;

	
	public static Boolean CHECK_FLG = false;
	
	/**
	 * @author sky Email vipa1888@163.com QQ:840950105 获取当前的网络状态 -1：没有网络
	 *         1：WIFI网络2：wap网络3：net网络
	 * @param context
	 * @return
	 */
	public static int getAPNType(Context context) {

		int netType = -1;

		ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		if (networkInfo == null) {
			return netType;
		}
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_MOBILE) {
			if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
				netType = CMNET;
			} else {
				netType = CMWAP;
			}
		} else if (nType == ConnectivityManager.TYPE_WIFI) {
			netType = WIFI;
		}
		return netType;
	}

	// private void showTips() {
	// AlertDialog alertDialog = new AlertDialog.Builder(ActivityLogin.this)
	// .setTitle("提示").setMessage("确定退出程序?")
	// .setPositiveButton("确定", new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int which) {
	// ActivityLogin.this.finish();
	// }
	// })
	// .setNegativeButton("取消", new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int which) {
	// return;
	// }
	// }).create(); // 创建对话框
	// alertDialog.show(); // 显示对话框
	// }
	
	 public static void showTips(final Activity ac1) {
		AlertDialog alertDialog = new AlertDialog.Builder(ac1)
				.setTitle("提示")
				.setMessage("确定退出二维码扫描?")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						 Intent intent = new Intent();
//						 intent.setClass(ac1,
//								 IndexActivity.class);// 为当前界面的Activity，Activity2为要跳转的类
						 ac1.startActivity(intent);
//			    	     int pid = android.os.Process.myPid();
//		                 android.os.Process.killProcess(pid);
						 ac1.finish();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				}).create(); // 创建对话框
		alertDialog.show(); // 显示对话框
	}
	
	public static void AlertCheck(final Activity activit, String btnok,
			String btnclose, String msg) {
		AlertDialog alertDialog = new AlertDialog.Builder(activit)
				.setTitle(btnclose)
				.setMessage(msg)
//				.setPositiveButton(btnok,
//						new DialogInterface.OnClickListener() {
//							public void onClick(DialogInterface dialog,
//									int which) {
//								activit.finish();
//							}
//						})
				.setNegativeButton(btnok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								return;
							}
						}).create(); // 创建对话框
		alertDialog.show(); // 显示对话框
	}

	public static void AlertCheck(final Activity activit, String btnok,
			String msg) {
		AlertDialog alertDialog = new AlertDialog.Builder(activit)
				.setTitle("提示")
				.setMessage(msg)
				.setPositiveButton(btnok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								return;
							}
						}).create(); // 创建对话框
		alertDialog.show(); // 显示对话框
	}

	// 放大缩小图片
	public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidht = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidht, scaleHeight);
		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		return newbmp;
	}
    /**
     * 放小旋转图片
     * @param degree 旋转的角度
     * @param bitmap 图片
     * @return newbmp 放小后旋转好的图片
     */
	public static Bitmap zoomBitmap(int degree,Bitmap bitmap) {
		//旋转
		Bitmap bmplod = rotaingImageView(degree, bitmap);
		int w = 360;
		int h = 480;
		int width = bmplod.getWidth();
		int height = bmplod.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidht = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidht, scaleHeight);
		Bitmap newbmp = Bitmap.createBitmap(bmplod, 0, 0, width, height,matrix, true);
		return newbmp;
	}

	// 将Drawable转化为Bitmap
	public static Bitmap drawableToBitmap(Drawable drawable) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
				.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas);
		return bitmap;
		// Generated by Foxit PDF Creator © Foxit Software
		// http://www.foxitsoftware.com For evaluation only.
	}

	// 获得圆角图片的方法
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	// 获得带倒影的图片方法
	public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
		final int reflectionGap = 4;
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		matrix.preScale(1, -1);
		Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
				width, height / 2, matrix, false);
		Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
				(height + height / 2), Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmapWithReflection);
		canvas.drawBitmap(bitmap, 0, 0, null);
		Paint deafalutPaint = new Paint();
		// Generated by Foxit PDF Creator © Foxit Software
		// http://www.foxitsoftware.com For evaluation only.
		canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
		canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
				bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
				0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		// Set the Transfer mode to be porter duff and destination in
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		// Draw a rectangle using the paint with our linear gradient
		canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
				+ reflectionGap, paint);
		return bitmapWithReflection;
	}

    /**
     * 读取图片属性：旋转的角度
     * @param path 图片绝对路径
     * @return degree旋转的角度
     */
	public static int readPictureDegree(String path) {
	    int degree  = 0;
	    try {
	            ExifInterface exifInterface = new ExifInterface(path);
	            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
	            switch (orientation) {
	            case ExifInterface.ORIENTATION_ROTATE_90:
	                    degree = 90;
	                    break;
	            case ExifInterface.ORIENTATION_ROTATE_180:
	                    degree = 180;
	                    break;
	            case ExifInterface.ORIENTATION_ROTATE_270:
	                    degree = 270;
	                    break;
	            }
	    } catch (IOException e) {
	            e.printStackTrace();
	    }
    return degree;
} 
	/**
	 * 旋转图片
	 * @param angle
	 * @param bitmap
	 * @return Bitmap
	 */
	public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {
        //旋转图片 动作
		Matrix matrix = new Matrix();;
        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		return resizedBitmap;
	}
	
//	// Bitmap → byte[]
//	private byte[] Bitmap2Bytes(Bitmap bm) {
//		ByteArrayOutputStream baos = new ByteArrayOutputStream();
//		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
//		return baos.toByteArray();
//	}
//	//byte[] → Bitmap
//	private Bitmap Bytes2Bimap(byte[] b) {
//		if (b.length != 0) {
//			return BitmapFactory.decodeByteArray(b, 0, b.length);
//		} else {
//			return null;
//		}
//	}
}
